package com.campus.mapper.Impl;

import com.campus.domain.Dto.TeacherAddDto;
import com.campus.domain.Vo.TeacherSelectVo;
import com.campus.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class TeacherMapperImpl implements TeacherMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer addTeacher(TeacherAddDto teacherAddDto, Long loginId) {
        String sql = "insert into t_manager_teacher (teacher_name,subject,profession,school,introduce,login_id) values (?,?,?,?,?,?)";
        Object[] args = new Object[]{
                teacherAddDto.getTeacherName(),
                teacherAddDto.getSubject(),
                teacherAddDto.getProfession(),
                teacherAddDto.getSchool(),
                teacherAddDto.getIntroduce(),
                loginId
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public TeacherSelectVo selectById(Long id) {
        String sql = "select * from t_manager_teacher where id = ?";

        Object[] args = new Object[]{
                id
        };
        return jdbcTemplate.query(sql,args,rs -> {
            TeacherSelectVo teacher = new TeacherSelectVo();
            while (rs.next()){
                teacher.setId(rs.getLong("id"));
                teacher.setTeacherName(rs.getString("teacher_name"));
                teacher.setSubject(rs.getString("subject"));
                teacher.setProfession(rs.getString("profession"));
                teacher.setSchool(rs.getString("school"));
                teacher.setIntroduce(rs.getString("introduce"));
            }
            return teacher;
        });
    }
}
